from django.shortcuts import render, redirect, reverse

# Create your views here.

from django.http import HttpResponse, JsonResponse

# '''
# 重定向
# '''
# def index(request):
#     # 如果没有登陆，那么重定向到注册页面
#     # /?username=xxx
#     username = request.GET.get("username")
#     if username:
#         return HttpResponse("success")
#     else:
#         return redirect(reverse('front2:signup'))
#
# def signup(request):
#     return HttpResponse("signup")
from django.views.decorators.http import require_http_methods

def index(request):
    # username = request.GET('username')    # 没有值会报错
    # username = request.GET.get('p', default=1)   # default 默认值
    # print(username)
    # return HttpResponse("index")
    response =  HttpResponse("<h1>知了</h1>",content_type='text/plain;charset=utf-8')
    response['X-Token'] = 'zhiliao'
    return response

@require_http_methods(['GET','POST'])
def add_article(request):
    if request.method == 'GET':
        return render(request, 'front2/add_article.html')
    else:
        title = request.POST.get("title")
        content = request.POST.get("content")
        tags = request.POST.getlist("tags")
        print({'title':title})
        print({'content':content})
        print({'tags':tags})
        return HttpResponse("success")

# def login(request):
#     print(type(request))
#     print(request.path)     # path地址
#     print(request.get_raw_uri())    # 全部url
#     for key,value in request.META.items():      # 所有请求头信息
#         print(f"{key}:{value}")
#     print(request.get_host())   # 请求地址
#     print(request.is_secure())   #
#     print(request.is_ajax())    # 是否为ajax请求
#     return HttpResponse("login")

def jsonresponse_views(request):
    person=[{
        'username':'zhiliao',
        'age':'10',
        'height':180,
    },{
        'username':'zhiliao',
        'age':'10',
        'height':180,
    }]
    return JsonResponse(person,safe=False)